package cn.xjt.TcpFileLoad;

import java.io.*;
import java.net.Socket;

/**
 * @author xujiangtao
 * @create 2020-08-04-14:51
 */
public class ClientFileUpload {
    public static void main(String[] args) throws IOException {
        String str = null;

        Socket client = new Socket("192.168.137.1",9999);
        //得到一个网络输入流
        InputStream inputStream = client.getInputStream();
        //将读取到的输入流，发送到服务器
        OutputStream outputStream = client.getOutputStream();
        FileInputStream is =new FileInputStream(new File("E:\\AI练习\\苹果.ai"));
        byte[] data = new byte[5000];
        int len=-1;
        int count =0;
        while ((len=is.read(data))!=-1)
        {
            count++;
            outputStream.write(data,0,len);
            System.out.println("我一直在循环");
            System.out.println(len+"#########################");
        }
        //上传完文件，给服务器写一个结束标记
        client.shutdownOutput();        //禁用次套接字的输出流，结束了
        //对于一个TCP套接字，先前写入的数据将被发送后正常关闭TCP连接。如果你向一个socket写入输出流后调用shutdownoutput()插座上，流将抛出一个异常。

        System.out.println("我出来了"+count+"次循环");
        byte[] upload = new byte[1024];
        int leng = inputStream.read(upload);
        System.out.println(new String(upload,0,leng));
    }
}
